﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Text;
using System.Threading.Tasks;
using SerializationHelper;
namespace SerializableExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Person _person = new Person();
            _person.Name = "YanZhiwei";
            _person.Age = 100;
            _person.Address = "Shanghai";
            _person.Remark = ".NET Programmer";
            Person _personNormal = new Person();
            _personNormal.Name = "Normal";
            _personNormal.Age = 100;
            _personNormal.Address = "Shanghai";
            _personNormal.Remark = "Normal";
            IList<Person> _persons = new List<Person>();
            _persons.Add(_person);
            _persons.Add(_personNormal);
            try
            {
                byte aa = 0x3f;
                Console.WriteLine(Convert.ToString(aa, 2).PadLeft(6, '0'));
                Console.ReadLine();
                //            DoSerializableWork _doSerializableWorkToBinary = new DoSerializableWork(
                //              new SerializationOperate(string.Format(@"C:\{0}SerializableTest.bin", DateTime.Now.ToString("yyyyMMddHH"))));
                //            _doSerializableWorkToBinary.SerializationObject(_person, SerializationType.Binary);
                //            _doSerializableWorkToBinary.DeSerializationObject(SerializationType.Binary);
                //            Console.WriteLine("-----------------------------------------");

                //            DoSerializableWork _doSerializableWorkToSoap = new DoSerializableWork(
                //                new SerializationOperate(string.Format(@"C:\{0}SerializableTest.soap", DateTime.Now.ToString("yyyyMMddHH"))));
                //            _doSerializableWorkToSoap.SerializationObject(_person, SerializationType.SOAP);
                //            _doSerializableWorkToSoap.DeSerializationObject(SerializationType.SOAP);
                //            Console.WriteLine("-----------------------------------------");

                //            DoSerializableWork _doSerializableWorkToXML = new DoSerializableWork(
                //                new SerializationOperate(string.Format(@"C:\{0}SerializableTest.xml", DateTime.Now.ToString("yyyyMMddHH"))));
                //            _doSerializableWorkToXML.SerializationObject(_person, SerializationType.XML);
                //            _doSerializableWorkToXML.DeSerializationObject(SerializationType.XML);
                //            Console.WriteLine("-----------------------------------------");

                //            DoSerializableColWork _doSerColWorkToXML = new DoSerializableColWork(
                //new SerializationCollectionOperate(string.Format(@"C:\{0}SerializableCollectionTest.xml", DateTime.Now.ToString("yyyyMMddHH"))));
                //            _doSerColWorkToXML.SerializationCollection(_persons, SerializationCollectionType.XML);
                //            _doSerColWorkToXML.DeSerializationBinaryToCollection(SerializationCollectionType.XML);
                //            Console.WriteLine("-----------------------------------------");

                //            DoSerializableColWork _doSerColWorkToBinary = new DoSerializableColWork(
                //                new SerializationCollectionOperate(string.Format(@"C:\{0}SerializableCollectionTest.bin", DateTime.Now.ToString("yyyyMMddHH"))));
                //            _doSerColWorkToBinary.SerializationCollection(_persons, SerializationCollectionType.Binary);
                //            _doSerColWorkToBinary.DeSerializationBinaryToCollection(SerializationCollectionType.Binary);
                //            Console.WriteLine("-----------------------------------------");



            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.Trim());
            }
        }
    }

    public class DoSerializableColWork
    {
        public void aa<T>(IList<T> collection, IEnumerable<T> text)
        {

        }
        ISerializationCollectionHelper _serializableHelper = null;
        public DoSerializableColWork(ISerializationCollectionHelper serializableHelper)
        {
            if (serializableHelper == null)
                throw new ArgumentNullException("serializableHelper");
            _serializableHelper = serializableHelper;
        }
        public void SerializationCollection(IList<Person> persons, SerializationCollectionType type)
        {
            if (persons == null)
                throw new ArgumentNullException("persons");
            _serializableHelper.SerializationCollection<Person>(persons, type);
            Console.WriteLine(string.Format("Serialization Collection to {0} success.", type));
        }

        public void DeSerializationBinaryToCollection(SerializationCollectionType type)
        {
            IList<Person> _persons = _serializableHelper.DeSerializationCollection<Person>(type);
            if (_persons == null)
            {
                Console.WriteLine(string.Format("DeSerialization  {0} failure.", type));
            }
            else
            {
                foreach (Person _person in _persons)
                {
                    Console.WriteLine(string.Format("Name:{0}\n\rAge:{1}\n\rAddress:{2}\n\rRemark:{3}",
                    _person.Name,
                    _person.Age,
                    _person.Address,
                    _person.Remark));
                }
            }
        }
    }
    public class DoSerializableWork
    {
        ISerializationObjectHelper _serializableHelper = null;
        public DoSerializableWork(ISerializationObjectHelper serializableHelper)
        {
            if (serializableHelper == null)
                throw new ArgumentNullException("serializableHelper");
            _serializableHelper = serializableHelper;
        }
        public void SerializationObject<T>(T t, SerializationType type)
        {
            _serializableHelper.SerializationObject<T>(t, type);
            Console.WriteLine(string.Format("Serialization {0} to {1} success.", t.GetType().Name, type));
        }
        public void DeSerializationObject(SerializationType type)
        {
            Person _person = _serializableHelper.DeSerializationObject<Person>(type);
            if (_person == null)
            {
                Console.WriteLine(string.Format("DeSerialization {0} to {1} failure.", _person.GetType().Name, type));
            }
            else
            {
                Console.WriteLine(string.Format("Name:{0}\n\rAge:{1}\n\rAddress:{2}\n\rRemark:{3}",
                    _person.Name,
                    _person.Age,
                    _person.Address,
                    _person.Remark));
            }
        }

    }
    [Serializable]
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Address { get; set; }
        public string Remark { get; set; }
    }
}
